﻿namespace PlugNT.Common
{
    using System;
    using System.Text.RegularExpressions;

    public class RegHtml
    {
        public static string Help()
        {
            return "mailto:354990393@qq.com";
        }

        public static string[] MatcheComplexs(string str, string flag, string specialCode)
        {
            if ((str == null) || (str.Trim() == ""))
            {
                return new string[] { "(2)字符串为空，没有找到匹配的字符串！" };
            }
            string pattern = "(?isx)\r\n                      <({0})\\b\\s*{1}\\s*[^>]*>         #开始标记“<tag...>”\r\n                          (?>                         #分组构造，用来限定量词“*”修饰范围 \r\n                              <\\1[^>]*>  (?<Open>)    #命名捕获组，遇到开始标记，入栈，Open计数加1\r\n                          |                           #分支结构\r\n                              </\\1>  (?<-Open>)       #狭义平衡组，遇到结束标记，出栈，Open计数减1\r\n                          |                           #分支结构\r\n                              (?:(?!</?\\1\\b).)*       #右侧不为开始或结束标记的任意字符\r\n                          )*                          #以上子串出现0次或任意多次\r\n                          (?(Open)(?!))               #判断是否还有'OPEN'，有则说明不配对，什么都不匹配\r\n                      </\\1>                           #结束标记“</tag>”\r\n                     ";
            MatchCollection mc = new Regex(string.Format(pattern, Regex.Escape(flag), Regex.Escape(specialCode)), RegexOptions.Multiline | RegexOptions.IgnoreCase).Matches(str);
            string[] tStr = new string[mc.Count];
            int i = 0;
            foreach (Match ma in mc)
            {
                tStr[i] = ma.Groups[0].Value;
                i++;
            }
            return tStr;
        }

        public static string MatcheInners(string str, string flag)
        {
            if ((str == null) || (str.Trim() == ""))
            {
                return "(4)字符串为空，没有找到匹配的字符串！";
            }
            string start = Regex.Match(str, "<" + flag + @"[\s\S]*?>", RegexOptions.IgnoreCase).ToString();
            string end = "</" + flag + ">";
            string reStr = str.Substring(start.Length);
            return ((reStr.Length > end.Length) ? reStr.Substring(0, reStr.Length - end.Length) : reStr);
        }

        public static string[] MatcheSinglelines(string str, string flag)
        {
            if ((str == null) || (str.Trim() == ""))
            {
                return new string[] { "(1)字符串为空，没有找到匹配的字符串！" };
            }
            MatchCollection mc = new Regex("<" + flag + @"[\s\S]*?>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Matches(str);
            string[] tStr = new string[mc.Count];
            int i = 0;
            foreach (Match ma in mc)
            {
                tStr[i] = ma.Groups[0].Value;
                i++;
            }
            return tStr;
        }

        public static string MatcheValues(string str, string property)
        {
            if ((str == null) || (str.Trim() == ""))
            {
                return "(3)字符串为空，没有找到匹配的字符串！";
            }
            int flg = str.IndexOf(">");
            if (flg <= 0)
            {
                return "";
            }
            string m = Regex.Match(str.Substring(0, flg + 1), property + "\\s*\\=\\s*['|\"].+?['|\"]", RegexOptions.IgnoreCase).ToString();
            int dflg = m.IndexOf("'");
            int sflg = m.IndexOf("\"");
            if (m == "")
            {
                m = Regex.Match(str, property + @"\s*\=\s*.+?[\s+|>]", RegexOptions.IgnoreCase).ToString();
                if (m != "")
                {
                    m = m.TrimStart(property.ToCharArray()).Trim().TrimStart(new char[] { '=' }).Trim().TrimEnd(new char[] { '>' });
                }
                return m;
            }
            if (dflg > 0)
            {
                return m.Substring(dflg).TrimStart(new char[] { '\'' }).TrimEnd(new char[] { '\'' });
            }
            return m.Substring(sflg).TrimStart(new char[] { '"' }).TrimEnd(new char[] { '"' });
        }
    }
}

